#include <iostream>

#include "gtest/gtest.h"
#include "ClientApi.h"
#include <memory>
#include "Log.h"
#include <vector>
#include "ClientApiImp.h"

TEST(Base, ConnectionSuccess) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api(CreateClientApi("127.0.0.1", "5001", "root", "root"));
        ASSERT_TRUE(api != NULL);
    });
}

TEST(Base, ConnectionFailHost1) {
    FUNCTION_TRACK
    ASSERT_ANY_THROW({
        boost::shared_ptr<ClientApi>  api(CreateClientApi("not-exists.pl", "5001", "root", "root"));
    });
}

TEST(Base, ConnectionFailHost2) {
    FUNCTION_TRACK
    ASSERT_ANY_THROW({
        boost::shared_ptr<ClientApi>  api(CreateClientApi("127.0.0.1", "5008", "root", "root"));
    });
}

TEST(Base, ConnectionFailUser) {
    FUNCTION_TRACK
    ASSERT_ANY_THROW({
        boost::shared_ptr<ClientApi>  api(CreateClientApi("127.0.0.1", "5001", "root-lol", "root"));
    });
}

TEST(Base, ConnectionFailPassword) {
    FUNCTION_TRACK
    ASSERT_ANY_THROW({
        boost::shared_ptr<ClientApi>  api(CreateClientApi("127.0.0.1", "5001", "root", "noroot"));
    });
}
/*
TEST(Base, FilesGet) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api(CreateClientApi("127.0.0.1", "5001", "root", "root"));
        std::vector<File> files = api->GetFilesInDir(0);
        EXPECT_EQ(files.size(), 0);
    });
}

TEST(Base, FoldersGet) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api( CreateClientApi("127.0.0.1", "5001", "root", "root"));
        std::vector<Folder> folders = api->GetFoldersInDir(0);
        EXPECT_EQ(folders.size(), 1);
    });
}

TEST(Base, FoldersCreate) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api( CreateClientApi("127.0.0.1", "5001", "root", "root"));
        std::vector<Folder> folders = api->GetFoldersInDir(0);

        EXPECT_EQ(folders.size(), 1);
        unsigned int id = api->MakeDirectory(folders[0].id, "Katalog");

        ASSERT_FALSE(id == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 2);

        ASSERT_FALSE(api->DeleteDirectory(id) != 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 1);
    });
}

TEST(Base, FoldersRename) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api( CreateClientApi("127.0.0.1", "5001", "root", "root"));
        std::vector<Folder> folders = api->GetFoldersInDir(0);

        EXPECT_EQ(folders.size(), 1);
        unsigned int id = api->MakeDirectory(folders[0].id, "Katalog");

        ASSERT_FALSE(id == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 2);

        ASSERT_FALSE(api->DeleteDirectory(id) == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 1);

    });
}


TEST(Base, FolderAndFileCreate) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api ( CreateClientApi("127.0.0.1", "5001", "root", "root"));
        std::vector<Folder> folders = api->GetFoldersInDir(0);

        EXPECT_EQ(folders.size(), 1);
        unsigned int id = api->MakeDirectory(folders[0].id, "Katalog");

        ASSERT_FALSE(id == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 2);

        unsigned int fileId = api->RenameDirectory(id, "plik.txt");
        ASSERT_FALSE(fileId == 0);

        ASSERT_FALSE(api->DeleteFile(fileId) == 0);

        ASSERT_FALSE(api->DeleteDirectory(id) == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 1);
    });
}

TEST(Base, OpenFile) {
    FUNCTION_TRACK
    ASSERT_NO_THROW({
        boost::shared_ptr<ClientApi> api(CreateClientApi("127.0.0.1", "5001", "root", "root"));
        std::vector<Folder> folders = api->GetFoldersInDir(0);

        EXPECT_EQ(folders.size(), 1);
        unsigned int folderId = api->MakeDirectory(folders[0].id, "Katalog");

        ASSERT_FALSE(folderId == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 2);

        unsigned int fileId = api->OpenFile(folderId, "plik.txt", "w");
        ASSERT_FALSE(fileId == 0);


        //EXPECT_EQ(api->OpenFile(fileId, string("r")), 0);
        //EXPECT_EQ(api->OpenFile(fileId + 1000, string("w")), -1);

        ASSERT_FALSE(api->DeleteFile(fileId) == 0);

        ASSERT_FALSE(api->DeleteDirectory(folderId) == 0);
        EXPECT_EQ(api->GetFoldersInDir(0).size(), 1);
    });
}*/